package com.bocai.dahuatech.ui.activity;

import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.SharePrefencesUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 修改密码
 */
public class ChangePsdActivity extends BaseActivity {

    @Bind(R.id.et_changepwd_oldpwd)
    EditText etOldpwd;
    @Bind(R.id.et_changepwd_newpwd)
    EditText etNewpwd;
    @Bind(R.id.et_changepwd_pwdtrue)
    EditText etNewpwdtrue;
    @Bind(R.id.btn_changepwd_commit)
    Button btnCommit;
    @Override
    protected int contentView() {
        return R.layout.activity_change_psd;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.changepsd);
        initEvent();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(etOldpwd.getText().toString())){
                    showToast(R.string.oldpsdnotnull);
                }else if(TextUtils.isEmpty(etNewpwd.getText().toString()))
                {
                    showToast(R.string.newpsdnotnull);

                }else if(etNewpwd.getText().length()<6)
                {
                    showToast(R.string.psdnot6);
                }else if(TextUtils.isEmpty(etNewpwdtrue.getText().toString()))
                {
                    showToast(R.string.psdtruenotnull);
                }else if(!etNewpwd.getText().toString().equals(etNewpwdtrue.getText().toString()))
                {
                    showToast(R.string.psdfalse);
                }else
                {
                    commit();
                }
            }
        });
    }

    private void commit() {
        showLoading();
//        etOldpwd.getText().toString();
       String oldpsd= AESUtils.md5(etOldpwd.getText().toString());
        Log.e("oldpsdmd5",oldpsd+"---------->"+etOldpwd.getText().toString());
        getTestApi().changePwd(SharePrefencesUtil.getFID(this), AESUtils.md5(etOldpwd.getText().toString()), AESUtils.md5(etNewpwd.getText().toString()))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
//        getTestApi().changePwd(SharePrefencesUtil.getFID(this),etOldpwd.getText().toString(), etNewpwd.getText().toString())
//                .compose(this.<BaseBean>bindToLifecycle())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(observer);
    }
    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {
            finish();
        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            Log.i("erro",e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean) {
            hideLoading();
            showToast(baseBean.getReturnInfo());
            SharedPreferences preferences=getSharedPreferences("user",MODE_PRIVATE);
            SharedPreferences.Editor editor=preferences.edit();
            editor.putString("password", AESUtils.md5(etNewpwdtrue.getText().toString()));
            editor.commit();
        }
    };
}
